/*global location: false, YAHOO: false, jQuery: false, SelectList: false, not_default_columns: false, window: false, Results: false, RECORDS_PER_PAGE: false, ResultsColumnDefs: false, emptyMessage: false, filter_options: true, ResultsDataTableDeferred: false */

/*members argument, failure, get, getColumn, getColumnSet, getData, 
    getLength, getRecord, getRecordSet, getState, getTime, hide, html, key, 
    keys, label, length, onDataReturnInitializeTable, onDataReturnSetRows, 
    reload, reset, results, scope, sendRequest, show, substr, success, text, 
    view_as
*/

function update_filters_div() {
    var filter_s = "",
        k;

    for (k in filter_options) {
        if ((k.substr(0, 4) !== 'col_') && filter_options[k]) {
            filter_s += ResultsDataTableDeferred.getColumn(k).label + ': ' + filter_options[k] + ', ';
        }
    }

    if ("" !== filter_s) {
        jQuery("#filters").html('for <strong>' + filter_s.substr(0, filter_s.length - 2) + '</strong>');
    } else {
        jQuery("#filters").text('');
    }
}


/* begin: Table object */
var Table = {

    /* Reset table */
    reset: function () {

        var oState = ResultsDataTableDeferred.getState(),
            request, oCallback;
           

        request = ResultsDataTableDeferred.get("generateRequest")(oState, ResultsDataTableDeferred);
        oCallback = {
            success: function (sRequest, oResponse, oPayload) {
                var returned_results_count = oResponse.results.length,
                    end_index = RECORDS_PER_PAGE;
                ResultsDataTableDeferred.onDataReturnInitializeTable(sRequest, oResponse, oPayload);
                jQuery("#img_loading").hide(); // Hide 'Loading' image (blue rolling circle)
                // Update count of records in paragraph "Showing ... of ..."
                if (returned_results_count < RECORDS_PER_PAGE) {
                    end_index = returned_results_count;
                }
                jQuery('#records_index').text('1-' + end_index + ' of ');
                // Update filter options in paragraph "Showing ... of ..."
                update_filters_div();
            },
            failure: function (sRequest, oResponse, oPayload) {
                ResultsDataTableDeferred.onDataReturnInitializeTable(sRequest, oResponse, oPayload);
                jQuery("#img_loading").hide();
            },
            argument: oState,
            scope: ResultsDataTableDeferred
        };
        jQuery("#img_loading").show();
        Results.sendRequest(request, oCallback);
    },

    /* Reload a table, e.g. when user adds or removes column */
    reload: function () {
        var request, oCallback, oState = ResultsDataTableDeferred.getState();
        request = ResultsDataTableDeferred.get("generateRequest")(oState, ResultsDataTableDeferred);
        oCallback = {
            success: function (sRequest, oResponse, oPayload) {
                ResultsDataTableDeferred.onDataReturnSetRows(sRequest, oResponse, oPayload);
                jQuery("#img_loading").hide();
            },
            failure: function (sRequest, oResponse, oPayload) {
                ResultsDataTableDeferred.onDataReturnSetRows(sRequest, oResponse, oPayload);
                jQuery("#img_loading").hide();
            },
            argument: oState,
            scope: ResultsDataTableDeferred
        };
        jQuery("#img_loading").show();
        Results.sendRequest(request, oCallback);
    },

    /* Construct HTML code of table for current results set  */
    view_as: function (v) {

        if (v === 'html') {
            var recordSet = ResultsDataTableDeferred.getRecordSet(),
                columns = ResultsDataTableDeferred.getColumnSet().keys,
                l = recordSet.getLength(),
                i, d, n, t, k, timestamp = new Date().getTime(),
                // Construct HTML code for table
            table_html = '<p>&nbsp;</p>\n<table class="company_search_results" id="company_search_results_' + timestamp + '">\n\n<thead>\n\n<tr>\n';
            // Add column headers
            for (i = 0; i < columns.length; i += 1) {
                table_html += '<th>' + columns[i].label + '</th>';
            }
            table_html += '</tr>\n</thead>\n\n';
            // Add rows
            for (i = 0; i < l; i += 1) {
                table_html += "<tr>\n";
                for (n = 0; n < columns.length; n += 1) {
                    table_html += '\t<td valign="top">';
                    d = recordSet.getRecord(i).getData(columns[n].key) || '&nbsp;';
                    if (typeof(d) === 'object') {
                        t = '';
                        for (k = 0; k < d.length; k += 1) {
                            if (d[k]) {
                                t = t + '<li>' + d[k] + '</li>';

                            }
                        }
                        table_html += '<ul>' + t + '</ul>';
                    } else {
                        table_html += d;
                    }
                    table_html += '</td>\n';
                }
                table_html += '</tr>\n\n';
            }
            table_html += '</table>';
            /* This is need for displaying table with Yahoo Interface in Analysis page */
            table_html += '<div id="tc_company_search_results_' + timestamp + '">Don\'t remove this placeholder. It will be replaced.</div>\n<p>&nbsp;</p>\n';
            return table_html;
        } else {
            return false;
        }
    }
};
/* end: Table object */
